<?php

class Controller extends controllerBase {

	public function index()
	{
		$errors = array();
		
		if(isset($_POST['submit']))
		{
			//form has been sent; process
			
			if($_POST['subject'] == '')
			{
				$errors[] = 'no-subject';
			}
			
			if($_POST['message'] == '')
			{
				$errors[] = 'no-message';
			}
			
			if (!clean::checkEmail($_POST['email']))
			{
				$errors[] = 'invalid-email';
			}
			
		
	
	
			if(count($errors) == 0)
			{	
				//send mail!
				
				require_once ROOT . "includes/lib/Swift.php";
				require_once ROOT. "includes/lib/Swift/Connection/SMTP.php";
			 
				//Start Swift
				$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
				 
				//Create the message
				$message =& new Swift_Message("NHHC :: " . $_POST['subject'], $_POST['message']);
				 
				//Now check if Swift actually sends it
				if ($swift->send($message, $_POST['to'], $_POST['email'])) $submitted = true;
				else $errors[] = 'send-fail';

			}
		}
		
		
		if($submitted)
		{
			//redirect
			echo display::header("Contacts");
			echo display::menu("contacts");
			echo display::redirect("Message Sent.");
			echo display::footer();
		}
		else
		{
			//show the form
			echo display::header("Mailing List");
			echo display::menu("contacts");
			echo form::contactForm($errors);
			echo display::footer();
		}
	}
}